<?php
function displayPage()
{
	/*GET VARS:
		- id: DB ID of element to be modified ('add' value to add new content)
		- subcat: SubCategory of object to be modified
	*/
	//--- VARS
	$state = getData('site_content', 'title', 'contentID = "'.get('id').'"');
	$linkback = INDEX.'cat'.EXT.'cat='.get('subcat');

	if ($state['title'][0] == 'null' || $state['title'][0] == 'Add')
	{
		//add
		$give = 'Add New Product';
		$page = 'Add';
		$titleText = '';
		$descText = '';
	}
	else
	{
		
		//edit
		$give = 'Edit Content: '.str_replace("_", " ", $state['title'][0]);
		$page = get('id');
		$titleText = $state['title'][0];
		$descTextDB = getData('site_content', 'contdata', 'contentID = "'.get('id').'"');
		$descText = $descTextDB['contdata'][0];
	}
	
	if(post('updateNow') != 'daf6h4Wr' || (post('title') == '' || post('title') == 'null') || (post('contentText')=='' || post('contentText') == 'null'))
	{
		$display= '<div id="container">
			<div id="header">
			'.$give.'
			</div>
			<div id="formDiv">
			<form enctype="multipart/form-data" method="POST" action="widget.php?page=gallery&id='.$page.'&subcat='.get('subcat').'" onsubmit="'.jsConfirm('Are you sure you want to continue saving this entry? This action cannot be undone.').'"><input type="hidden" name="updateNow" value="daf6h4Wr" /><input type="hidden" name="formId" value="'.crypt('emf27musedib20ages', 'saltriotikistic28').'" />
			Title: <input type="text" name="title" value="'.$titleText.'" id="titleForm"/>
			Image: <input type="file" name="fileImg" id="fileForm" /><br />
			Note: Only *.jpg or *.jpeg files are allowed to be uploaded. Uploading other file types may result in error.
			</div>
			'.textEditor('contentEditor', 'contentText', $descText, 'Save').'</form>&nbsp;<input type="button" class="buttons" title="'.$linkback.'" value="Close" onclick="'.jsClose().'" />
		</div>';
	}
	else
	{
		$display = '<div id="container">
				<div id="header">
				'.$give.'
				</div>
				<div id="confirmationText">
					<br />The content data for this element has been updated today '.getDateNow().'.
				</div>
				<div style="text-align: center;">
				
				<input type="button" class="buttons" title="'.$linkback.'" value="Close" onclick="'.jsClose().'" />
				</div>
			</div>
		';
		
		//ADD/UPDATE HERE!
		if(get('id') == 'Add')
		{
			if(!isset($_FILES['fileImg']['size']) || $_FILES['fileImg']['size'] <= 0)
			{
				$display = '<div id="container">
						<div id="header">
						'.$give.'
						</div>
						<div id="confirmationText">
							<br />There was an error uploading the file. Please specify a file name. There were no actions accomplished.
						</div>
						<div style="text-align: center;">
						
						<input type="button" class="buttons" title="'.$linkback.'" value="Close" onclick="'.jsClose().'" />
						</div>
					</div>
				';
			}
			else
			{
				$targPath['full'] = BASED.'img/products/full/'.get('subcat').'/';
				$targPath['thumb'] = BASED.'img/products/thumb/'.get('subcat').'/';
				$fileNewName = str_replace(" ", "_", post('title'));
				$fileNameExt = explode(".", $_FILES['fileImg']['name']);
				$fileReceive = receiveFile('fileImg', $fileNewName, $fileNameExt[count($fileNameExt)-1], $targPath['full'], post('formId'));
				
				//RESIZE TO CREATE THUMBNAIL
				list($imgWidth, $imgHeight, $imgType, $imgAttr) = getimagesize($targPath['full'].post('title').'.'.$fileNameExt[count($fileNameExt)-1]);
				
				$src = imagecreatefromjpeg($targPath['full'].post('title').'.jpg');
				$dest = imagecreatetruecolor(195,98);
				
				
				$cropWidth = round(($imgWidth >= $imgHeight ? $imgWidth: ($imgHeight * 195)/98));
				$cropHeight = round(($imgWidth >= $imgHeight ? ($imgWidth * 98)/195 : $imgHeight));
				
				$srcCutW = ($cropWidth == $imgWidth ? 0 : ($imgWidth/2)-($cropWidth/2));
				$srcCutH = ($cropWidth != $imgWidth ? 0 : ($imgHeight/2)-($cropHeight/2));
				
				
				imagecopyresampled($dest, $src, 0, 0, $srcCutW, $srcCutH, 195, 98, $cropWidth, $cropHeight);
				
				
				imagejpeg($dest, $targPath['thumb'].str_replace(" ", "_", post('title')).'.jpg');
				
				imagedestroy($src);
				imagedestroy($dest);
				
				$tableName = 'site_content';
				
				$fields = array();
				$fields[0] = 'contName';
				$fields[1] = 'title';
				$fields[2] = 'contdata';
				$fields[3] = 'conttype';
				$fields[4] = 'perPage';
				$fields[5] = 'dateUpdated';
				
				$values = array();
				$values[0] = get('subcat');
				$values[1] = str_replace(" ", "_", post('title'));
				$values[2] = post('contentText');
				$values[3] = 'gallery';
				$values[4] = '0';
				$values[5] = sqlDateNow();
				
				$data = SQLInsert($tableName, $fields, $values);
				
			}
		}
		else
		{
			//UPDATE FUNCTIONS
			if ($state['title'][0] == 'null' )
			{
				$display='<script type="text/javascript">
					<!--
					window.close();
					-->
				</script>';
			}
			else
			{
				$targPath['full'] = BASED.'img/products/full/'.get('subcat').'/';
				$targPath['thumb'] = BASED.'img/products/thumb/'.get('subcat').'/';
				if(!isset($_FILES['fileImg']['size']) || $_FILES['fileImg']['size'] <= 0)
				{
					//RENAME FILE
					rename($targPath['full'].$state['title'][0].'.jpg', $targPath['full'].str_replace(" ", "_", post('title')).'.jpg');
					rename($targPath['thumb'].$state['title'][0].'.jpg', $targPath['thumb'].str_replace(" ", "_", post('title')).'.jpg');
				}
				else
				{
					
					$fileNewName = post('title');
					$fileNameExt = explode(".", $_FILES['fileImg']['name']);
					$fileReceive = receiveFile('fileImg', $fileNewName, $fileNameExt[count($fileNameExt)-1], $targPath['full'], post('formId'));
					
					//RESIZE TO CREATE THUMBNAIL
					list($imgWidth, $imgHeight, $imgType, $imgAttr) = getimagesize($targPath['full'].post('title').'.'.$fileNameExt[count($fileNameExt)-1]);
					
					$src = imagecreatefromjpeg($targPath['full'].post('title').'.jpg');
					$dest = imagecreatetruecolor(195,98);
					
					
					$cropWidth = round(($imgWidth >= $imgHeight ? $imgWidth: ($imgHeight * 195)/98));
					$cropHeight = round(($imgWidth >= $imgHeight ? ($imgWidth * 98)/195 : $imgHeight));
					
					$srcCutW = ($cropWidth == $imgWidth ? 0 : ($imgWidth/2)-($cropWidth/2));
					$srcCutH = ($cropWidth != $imgWidth ? 0 : ($imgHeight/2)-($cropHeight/2));
					
					
					imagecopyresampled($dest, $src, 0, 0, $srcCutW, $srcCutH, 195, 98, $cropWidth, $cropHeight);
					
					
					imagejpeg($dest, $targPath['thumb'].str_replace(" ", "_", post('title')).'.jpg');
					
					imagedestroy($src);
					imagedestroy($dest);
				}
				$tableName = 'site_content';
					
				$fields = array();
				$fields[0] = 'contName';
				$fields[1] = 'title';
				$fields[2] = 'contdata';
				$fields[3] = 'conttype';
				$fields[4] = 'perPage';
				$fields[5] = 'dateUpdated';
				
				$values = array();
				$values[0] = get('subcat');
				$values[1] = str_replace(" ", "_", post('title'));
				$values[2] = post('contentText');
				$values[3] = 'gallery';
				$values[4] = '0';
				$values[5] = sqlDateNow();
				
				$whereClause = 'contentID="'.get('id').'"';
				
				$data = SQLUpdate($tableName, $whereClause, $fields, $values);
			}
		}
		
		
	}
	

	return $display;
}
?>